package forum.entities;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.math.BigInteger;

public class User extends Entity  {

    @NotNull(message = "email cannot be null")
    @Pattern(regexp = "^(?:[a-zA-Z0-9_'^&/+-])+(?:\\.(?:[a-zA-Z0-9_'^&/+-])+)" +
            "*@(?:(?:\\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\\.)" +
            "{3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\]?)|(?:[a-zA-Z0-9-]+\\.)" +
            "+(?:[a-zA-Z]){2,}\\.?)$",
            message = "incorrect email")
    private String email;

    @NotNull(message = "nickname cannot be null")
    @Size(min = 3, message = "nickname length must be at least 3 symbols")
    private String nickName;

    @NotNull(message = "password cannot be null")
    @Size(min = 5, message = "password length must be at least 5 symbols")
    private String password;

    public User(BigInteger id) {
        super(id);
        email = "";
        nickName = "";
        password = "";
    }

    public User(BigInteger id,String email,String nickName,String password) {
        super(id);
        this.email = email;
        this.nickName = nickName;
        this.password = password;
    }

    public User(User user) {
        super(user.getId());
        this.setEmail(user.getEmail());
        this.setNickName(user.getNickName());
        this.setPassword(user.getPassword());
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (! (o instanceof User)) return false;
        if (! super.equals(o)) return false;

        User user = (User) o;

        if (! email.equals(user.email)) return false;
        if (! nickName.equals(user.nickName)) return false;
        if (! password.equals(user.password)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + email.hashCode();
        result = 31 * result + nickName.hashCode();
        result = 31 * result + password.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "User{" +
                "email='" + email + '\'' +
                ", nickName='" + nickName + '\'' +
                ", password='" + password + '\'' +
                "} " + super.toString();
    }
}
